package reviewAlgorithms.search;

import java.util.ArrayList;
import java.util.List;

public class BinaryTreePaths {
    public static void main(String[] args) {

    }
    public static List<String> binaryTreePaths(TreeNode root) {
        List<String> res = new ArrayList<>();
        if (root == null) {
            return res;
        }
        List<Integer> oneRes = new ArrayList<>();
        List<List<Integer>> intRes = new ArrayList<>();
        getPath(root,oneRes,intRes);
        for (List<Integer> item : intRes) {
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < item.size(); i++) {
                stringBuilder.append(item.get(i));
                if (i < item.size() - 1){
                    stringBuilder.append("->");
                }
            }
            res.add(stringBuilder.toString());
        }
        return res;
    }

    private static void getPath(TreeNode root, List<Integer> oneRes, List<List<Integer>> intRes) {
        if (root == null) {
            return;
        }
        if (root.left == null && root.right == null){
            oneRes.add(root.val);
            List<Integer> temp = new ArrayList<>(oneRes);
            oneRes.remove(oneRes.size() - 1);
            intRes.add(temp);
            return;
        }
        oneRes.add(root.val);
        getPath(root.left,oneRes,intRes);
        getPath(root.right,oneRes,intRes);
        oneRes.remove(oneRes.size() - 1);
    }
}
